//
//  YMUIUtil.m
//  MyProject
//
//  Created by Pei on 16/6/19.
//  Copyright © 2016年 Xiaobo Pei. All rights reserved.
//

//自动消失提示框宽度
#define WIDTH_BORDER 233
//自动消失提示框高度
#define HEIGHT_BORDER 134

#import "MBProgressHUD.h"
#import "YMUIUtil.h"


@implementation YMUIUtil
{
    UIView *tipView;
    UIAlertView *alertView;
    BOOL needShowAlert;
    MBProgressHUD *progressHUD;
    MBProgressHUD *HUD;
    
}
//@synthesize tipView;
//@synthesize progressHUD;
//@synthesize alertView;
//@synthesize needShowAlert;
//@synthesize HUD;

SYNTHESIZE_SINGLETON_FOR_CLASS(YMUIUtil);


- (void)showLoadingWithTitle:(NSString *)title{
    progressHUD = nil;
    UIWindow *window = [[[UIApplication sharedApplication] windows] objectAtIndex:0];
    progressHUD = [[MBProgressHUD alloc] initWithWindow:window];
    [window addSubview:progressHUD];
    progressHUD.mode = MBProgressHUDModeIndeterminate;
    progressHUD.delegate = nil;
    progressHUD.labelText = title;
    [progressHUD show:YES];
}

- (void)myTask {
    // Do something usefull in here instead of sleeping ...
    sleep(3);
}
- (void)showAllTextDialog:(NSString *)title
{
    HUD = nil;
    UIWindow *window = [[[UIApplication sharedApplication] windows] objectAtIndex:0];
    HUD = [[MBProgressHUD alloc] initWithWindow:window];
    [window addSubview:HUD];
    HUD.mode = MBProgressHUDModeText;
    HUD.delegate = nil;
    HUD.labelText = title;
    
    HUD.margin = 10.f;
    HUD.removeFromSuperViewOnHide = YES;
    [HUD show:YES];
    [HUD hide:YES afterDelay:3];
    [NSTimer scheduledTimerWithTimeInterval:3.0f target:self selector:@selector(closeViewT) userInfo:nil repeats:NO];
}

- (void)closeViewT
{
    if (HUD != nil) {
        [HUD removeFromSuperview];
        HUD = nil;
    }
}

- (void)hideLoadingView
{
    if (progressHUD != nil) {
        [progressHUD removeFromSuperview];
        progressHUD = nil;
    }
}

- (void)closeView{
    if (tipView) {
        [tipView removeFromSuperview];
        tipView = nil;
        
        if ([self.delegate respondsToSelector:@selector(reshowAlertView)]) {
            [self.delegate reshowAlertView];
        }
    }
}

- (void)showTipViewAutoDis:(NSString *)title
{
    if (title != nil)
    {
        [self showTipViewAutoDisWithTitle:title withMsg:nil];
    }
    
}

- (void)showTipViewAutoDisWithTitle:(NSString*)title withMsg:(NSString*)msg
{
    
    if ([JackTool getIOSVersion] >= 7.0)
    {
        [self closeView];
        UIWindow *window = [[[UIApplication sharedApplication] windows] objectAtIndex:0];
        CGRect frame = window.bounds;
        CGRect showFrame;
       
        showFrame = CGRectMake((frame.size.width-WIDTH_BORDER)/2, (frame.size.height-HEIGHT_BORDER)/2, WIDTH_BORDER, HEIGHT_BORDER);
        
        tipView = [[UIView alloc] initWithFrame:frame];
        tipView.backgroundColor = [UIColor clearColor];
        //[self.tipView setBackgroundColor:[UIColor purpleColor]];
        [window addSubview:tipView];
        
        UIView *stringView = [[UIView alloc] init];
        [stringView setFrame:showFrame];
        UIColor *color = [UIColor colorWithWhite:0 alpha:0.6];
        [stringView setBackgroundColor:color];
        [stringView.layer setCornerRadius:8];
        [stringView.layer setMasksToBounds:YES];
        [tipView addSubview:stringView];
        
        UILabel *titleLB = [[UILabel alloc] initWithFrame:CGRectMake(20, 0, WIDTH_BORDER - 40, HEIGHT_BORDER)];
        [titleLB setBackgroundColor:[UIColor clearColor]];
        [titleLB setTextColor:[UIColor whiteColor]];
        titleLB.numberOfLines = 0;
        [titleLB setText:title];
        
        NSAttributedString *attrStr = [[NSAttributedString alloc] initWithString:titleLB.text];
        titleLB.attributedText = attrStr;
        NSRange range = NSMakeRange(0, attrStr.length);
        NSDictionary *dic = [attrStr attributesAtIndex:0 effectiveRange:&range];
        dic = @{NSFontAttributeName: [UIFont systemFontOfSize:17.0f]};
        [titleLB.text boundingRectWithSize:CGSizeMake(titleLB.frame.size.width, 0)
                                   options: NSStringDrawingTruncatesLastVisibleLine | NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading
                                attributes:dic
                                   context:nil];// 获取该段attributedString的属性字典
        
        titleLB.textAlignment = NSTextAlignmentCenter;
        [stringView addSubview:titleLB];
        
        UIButton *btn = [UIButton buttonWithType:UIButtonTypeSystem];
        [btn setFrame:frame];
        [btn setBackgroundColor:[UIColor clearColor]];
        [btn addTarget:self action:@selector(closeView) forControlEvents:UIControlEventTouchUpInside];
        [tipView addSubview:btn];
        [NSTimer scheduledTimerWithTimeInterval:2.0f target:self selector:@selector(closeView) userInfo:nil repeats:NO];
    }
    else
    {
        [self closeViewT];
        [self showAllTextDialog:title];
    }
}



-(void) clearAlertView{
    alertView = nil;
}

- (void)hideAlertView{
    if (alertView) {
        [alertView dismissWithClickedButtonIndex:0 animated:NO];
        needShowAlert = YES;
    }
}

- (void)showAlertViewAgain{
    if (alertView && needShowAlert == YES) {
        [alertView show];
        needShowAlert = NO;
    }
}

-(void)showAlertViewWithMessage:(NSString *)message cancelTitle:(NSString *)cancelString sureTitle:(NSString *)sureTitle AndDelete:(id)delete
{
    UIAlertView *alter = [[UIAlertView alloc] initWithTitle:@"提示" message:message delegate:delete cancelButtonTitle:cancelString otherButtonTitles:sureTitle, nil];
    [alter show];
}
@end
